/**
 * 
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.alee.laf.text.WebPasswordField;
import com.alee.laf.text.WebTextField;

import controller.Controls;

/**
 * Panel for user login.
 * 
 * @author
 */
public class LoginPanel extends JPanel implements KeyListener {
    /**
     * Serial Version ID.
     */
    private static final long serialVersionUID = 2607264486146393893L;
    /**
     * Application title
     */
    private static final String APP_TITLE = "Backgrounds/cursive_recipes_50.png";
    /**
     * Panel dimension.
     */
    private static final Dimension PANEL_DIM = new Dimension(200, 150);
    /**
     * 
     */
    private static final Dimension FILLER_DIM = new Dimension(200, 175);
    /**
     * Panel maximum dimension.
     */
    private static final Dimension PANEL_MAX = new Dimension(300, 250);
    /**
     * Field to enter password.
     */
    private WebPasswordField my_password_field;
    /**
     * Field to enter username.
     */
    private WebTextField my_username_input;
    /**
     * Button to submit login information.
     */
    private JButton my_submit_button;
    /**
     * Label to show what username field is for.
     */
    private JLabel my_username_label;
    /**
     * Label to show what password field is for.
     */
    private JLabel my_password_label;
    /**
     * New User label.
     */
    private JLabel my_new_user;
    /**
     * 
     */
    private BackgroundPanel my_title_panel;
    /**
     * 
     */
    private Controls my_controller;
    /**
     * 
     * @param the_controller
     */
    public LoginPanel(final Controls the_controller) {
        my_controller = the_controller;
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setPreferredSize(PANEL_DIM);
        setMaximumSize(PANEL_MAX);
        setOpaque(false);
        setup();
    }
    private void setup() {
        //Title
        my_title_panel = new BackgroundPanel(BackgroundPanel.createBackgroundImage(this, APP_TITLE), 0);
        my_title_panel.setPreferredSize(new Dimension(200, 100));
        my_title_panel.setMaximumSize(new Dimension(200, 100));
        my_title_panel.setOpaque(false);
        //Login
        my_password_field = new WebPasswordField();
        my_password_field.addKeyListener(this);
        my_username_input = new WebTextField();
        my_username_input.setColumns(10);
        my_username_input.addKeyListener(this);
        my_submit_button = new JButton("Login");
        my_submit_button.setBackground(new Color(128, 128, 128));
        my_submit_button.setForeground(Color.WHITE);
        my_submit_button.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), 
                                                                BorderFactory.createEmptyBorder(0, 22, 0, 22)));
        my_submit_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent the_event) {
                // Sends username and password to controller login on button
                // press.
                my_controller.login(my_username_input.getText(), new String(
                        my_password_field.getPassword()));
            }
        });
        my_username_label = new JLabel("Username", JLabel.RIGHT);
        my_password_label = new JLabel("Password", JLabel.RIGHT);
        //my_new_user = new JLabel("New User?", JLabel.RIGHT);
        my_new_user = new JLabel("         ", JLabel.RIGHT);
        my_username_label.setForeground(Color.WHITE);
        my_password_label.setForeground(Color.WHITE);
        my_new_user.setForeground(Color.WHITE);
        //Setup grid layout
        setupGrid();
    }
    public void setupGrid() {
        JPanel gridPanel = new JPanel();
        gridPanel.setOpaque(false);
        GridLayout grid = new GridLayout(3, 2);
        grid.setHgap(5);
        grid.setVgap(10);
        gridPanel.setLayout(grid);
        gridPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,50));
        gridPanel.setMaximumSize(new Dimension(240, 100));
        gridPanel.add(my_username_label);
        gridPanel.add(my_username_input);
        gridPanel.add(my_password_label);
        gridPanel.add(my_password_field);
        gridPanel.add(my_new_user);
        //gridPanel.add(my_submit_button);
        //final setup
        JPanel box = new JPanel();
        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.setOpaque(false);
        box.add(my_title_panel);
        box.add(gridPanel);
        box.setMaximumSize(PANEL_MAX);
        JPanel filler = new JPanel();
        filler.setOpaque(false);
        filler.setMaximumSize(FILLER_DIM);
        this.add(filler);
        this.add(box);
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyChar()==KeyEvent.VK_ENTER) {
            my_controller.login(my_username_input.getText(), 
                                new String(my_password_field.getPassword()));
        }      
    }
    @Override
    public void keyReleased(KeyEvent e) {
        
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
